Building an Interactive LED System: Arduino and Unity Integration

Building an Interactive LED System: Arduino and Unity Integration

In this project, I integrated Arduino and Unity to create an engaging LED control interface. The hardware component allows controlling in real-time the LEDs through buttons, a potentiometer, and serial commands, whereas the software part created in Unity depicts the state of the system and the potentiometer values in an animated form. Topics including important ideas, programming and elements that allow the system to work and be easy to use, are discussed in this blog.


Enhanced Mechanism

  • State Management: Button A toggles between "ON" and "OFF" states. The system defaults to "OFF" on startup.
  • Brightness Control: In the "ON" state, adjusting the potentiometer modifies LED A and LED B's brightness inversely.
  • Priority Switching: Button B toggles brightness control priority between the two LEDs.
  • Timeout Feature: If there is no interaction for 20 seconds or if Button A is pressed, the system returns to the "OFF" state.
  • Serial Command Modes:
    • MODE1: Control LEDs brightness through Button B clicks (2x, 3x, 4x for varying brightness, 5x to turn OFF).
    • MODE2: LEDs follow the state of Button B (press to turn ON, release to turn OFF).
    • EXIT: Exits the current mode and resets the LEDs to "OFF."

Hardware Implementation: Arduino Code

The Arduino code forms the backbone of the hardware interaction, handling button presses, potentiometer readings, LED brightness control, and serial communication. Below are the key features and important code sections:

1. State Management and Initialization

The system state (ON/OFF) and LED priority (LED A/LED B) are managed using boolean variables, ensuring a seamless user experience. Pins are defined and initialized for buttons, LEDs, and the potentiometer.

 

const int buttonAPin = 2;
const int buttonBPin = 3;
const int ledAPin = 5;
const int ledBPin = 6;
const int potentiometerPin = A3;
bool systemState = false;  // false = OFF, true = ON
bool ledABpriority = true; // if true, LED A increases with potentiometer value

    

2. Button Handling with Debouncing

Debouncing ensures that unintended button triggers are ignored, providing reliable input detection. Button A toggles the system state, while button B allows for mode-specific actions and LED priority switching.

void handleButtonAPress() {
    systemState = !systemState;
    if (systemState) {
        Serial.println("System is ON");
    } else {
        Serial.println("System is OFF");
    }
}
void handleButtonBPress() { if (systemState) { ledABpriority = !ledABpriority; Serial.println("LED priority switched"); } }

3. Potentiometer Control and LED Brightness

The potentiometer value is read and mapped to adjust the brightness of LEDs. Depending on the priority, either LED A or LED B increases in brightness as the potentiometer value changes.

 

void potentiometerCtl(int brightness) {
    if (ledABpriority) {
        ledABrightness = map(brightness, 0, 1023, 0, 255);
        ledBBrightness = map(brightness, 0, 1023, 255, 0);
    } else {
        ledABrightness = map(brightness, 0, 1023, 255, 0);
        ledBBrightness = map(brightness, 0, 1023, 0, 255);
    }
    analogWrite(ledAPin, ledABrightness);
    analogWrite(ledBPin, ledBBrightness);
}

    

4. Serial Command Processing

The system processes serial commands to activate modes or exit them, enabling dynamic interaction via the Unity interface.

if (setMode.equalsIgnoreCase("MODE1")) {
    mode1Callback();
} else if (setMode.equalsIgnoreCase("EXIT")) {
    systemState = false;
    analogWrite(ledAPin, 0);
    analogWrite(ledBPin, 0);
    Serial.println("Exited mode");
}

    

Software Integration: Unity C# Code

Unity serves as the user interface for the system, visualizing potentiometer values and LED states in real time. The C# script communicates with the Arduino via a serial port, dynamically updating the interface.

1. Real-Time Data Handling

The Update method continuously reads data from the Arduino and updates the potentiometer value and LED colors. Parsing the serial data ensures accurate representation.

 

if (data.StartsWith("P:")) {
    potentiometerValue = int.Parse(data.Substring(2));
    PotentiometerTxt.text = "Potentiometer Value: " + potentiometerValue;
}

    

2. LED Visualization

The brightness values of LED A and LED B are parsed and converted to colors, providing intuitive visual feedback in the Unity interface.

 

if (data.StartsWith("ABbri:")) {
    string[] brightnessValues = data.Substring(6).Split(',');
    float normalizedRed = int.Parse(brightnessValues[0]) / 255f;
    LEDA.color = new Color(normalizedRed, 0f, 0f);
    float normalizedGreen = int.Parse(brightnessValues[1]) / 255f;
    LEDB.color = new Color(0f, normalizedGreen, 0f);
}

    

3. User Interaction

Unity provides an input field for sending commands to the Arduino. For example, typing MODE1 activates a specific mode, while typing EXIT turns off the system.

 

public void ReadStringInput(string s) {
    if (arduinoPort.IsOpen) {
        arduinoPort.Write(s);
    }
    inputField.text = "";
}

    

Key Features and Applications

  • Real-Time Control: Adjust LED brightness and toggle states via buttons and serial commands.
  • User-Friendly Interface: Visualize system status and potentiometer values in Unity.
  • Mode Flexibility: Enable mode-specific functionalities like brightness presets.

Potential Applications:

  • Smart Home Systems: Dynamic lighting control via physical and digital inputs.
  • Educational Tools: Teaching concepts of analog data handling and system integration.
  • Prototyping IoT Devices: A base for developing advanced IoT systems.

Conclusion

This project demonstrates the synergy between hardware and software in creating an interactive LED control system. By combining the responsiveness of Arduino with Unity's visualization capabilities, I successfully bridged the gap between analog control and digital representation. This experiment serves as a stepping stone for exploring more complex applications in IoT, robotics, and smart systems.

 

Project information

Flag Counter